package view;

import java.util.Comparator;

import model.entity.Code;
import model.entity.ResourceEntity;

public class ResultadosResourcesModel extends AbstractResultadoModel<ResourceEntity> {

	private static final int COLUMNS_COUNT = 4;

	public static final String COLUMN_CODIGO_STR = "CODIGO";
	public static final String COLUMN_ORIGINAL_PATH_STR = "UBICACION ORIGINAL";
	public static final String COLUMN_DESTINATION_PATH_STR = "UBICACION CON MARCA";
	public static final String COLUMN_NOMBRE_STR = "NOMBRE";

	public static final int COLUMN_CODIGO = 0;
	public static final int COLUMN_ORIGINAL_PATH = 1;
	public static final int COLUMN_DESTINATION_PATH = 2;
	public static final int COLUMN_NOMBRE = 3;

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		switch (columnIndex) {
		case COLUMN_CODIGO:
			return Integer.class;
		case COLUMN_ORIGINAL_PATH:
			return String.class;
		case COLUMN_NOMBRE:
			return String.class;
		case COLUMN_DESTINATION_PATH:
			return String.class;
		}
		return null;
	}

	@Override
	public int getColumnCount() {
		return COLUMNS_COUNT;
	}

	@Override
	public String getColumnName(int columnIndex) {
		switch (columnIndex) {
		case COLUMN_CODIGO:
			return COLUMN_CODIGO_STR;
		case COLUMN_ORIGINAL_PATH:
			return COLUMN_ORIGINAL_PATH_STR;
		case COLUMN_NOMBRE:
			return COLUMN_NOMBRE_STR;
		case COLUMN_DESTINATION_PATH:
			return COLUMN_DESTINATION_PATH_STR;
		}
		return null;
	}

	@Override
	protected Object getValueAt(ResourceEntity object, int columnIndex) {
		switch (columnIndex) {
		case COLUMN_CODIGO:
			return object.getCode();
		case COLUMN_ORIGINAL_PATH:
			return object.getLocation();
		case COLUMN_NOMBRE:
			return object.getName();
		case COLUMN_DESTINATION_PATH:
			return object.getLocationMarked();
		}
		return null;
	}

	@Override
	protected void setValueAt(ResourceEntity object, Object value, int columnIndex) {
		switch (columnIndex) {
		case COLUMN_CODIGO:
			object.setCode((Code) value);
			break;
		case COLUMN_ORIGINAL_PATH:
			object.setLocation((String) value);
			break;
		case COLUMN_NOMBRE:
			object.setName((String) value);
			break;
		}
	}

	@Override
	protected void addComparatorsByColumn() {
		this.addComparator(COLUMN_CODIGO, new Comparator<ResourceEntity>() {
			@Override
			public int compare(ResourceEntity o1, ResourceEntity o2) {
				return o1.getCode().compareTo(o2.getCode());
			}
		});
		this.addComparator(COLUMN_ORIGINAL_PATH, new Comparator<ResourceEntity>() {
			@Override
			public int compare(ResourceEntity o1, ResourceEntity o2) {
				String uno = o1.getLocation() == null ? "" : o1.getLocation();
				String dos = o2.getLocation() == null ? "" : o2.getLocation();
				return uno.compareTo(dos);
			}
		});
		this.addComparator(COLUMN_NOMBRE, new Comparator<ResourceEntity>() {
			@Override
			public int compare(ResourceEntity o1, ResourceEntity o2) {
				String uno = o1.getName() == null ? "" : o1.getName();
				String dos = o2.getName() == null ? "" : o2.getName();
				return uno.compareTo(dos);
			}
		});
	}

}
